#!/bin/sh -f
# $Id: ntpd 7557b5d4d82b 2012/12/02 09:07:16 pgurumur $
# Copyright (c) 2009 Prabhu Gurumurthy  <pgurumur@gmail.com>
# 
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

# $Revision: 7557b5d4d82b $
# $Author: pgurumur $

# Program name
PROG=`basename $0`

# UID value
UID=`id -u`

# Globals
RUNDIR=
PID_FILE=
NTPD=`which ntpd`
NTPD_CONF="/etc/ntpd.conf"
LOGGER=`which logger`
AWK=`which awk`
SED=`which sed`

usage () {
   printf "${PROG} options [ commands ]\n"
   printf "options: \n"
   printf " -f [ specify ntp config file, default: '/etc/ntpd.conf' ]\n"
   printf " -h [ prints this help ]\n"
   printf "commands: \n"
   printf "\t start     [ Start OpenNTPD daemon ]\n"
   printf "\t stop      [ Stop OpenNTPD daemon ]\n"
   printf "\t status    [ Status of the OpenNTPD daemon ]\n"
   printf "\t restart   [ Restart OpenNTPD daemon ]\n"
   exit 1
}

count=0
ntpd_pid=
for value in `ps aux | egrep [n]tpd: | $SED -En 's/^[a-z_]+ +([0-9]+).*/\1/p'`;
do
   ntpd_pid[${count}]=${value}
   count=`expr $count + 1`
done

count=${#ntpd_pid[@]}

start_ntpd () {
   if [ ${#ntpd_pid[@]} -gt 0 ]; then
      ${LOGGER}: "${PROG} already running"
   else
      if [ -f ${NTPD} -a -x ${NTPD} ]; then
         ${NTPD} > /dev/null 2>&1
      else
         ${LOGGER} "${PROG} no such file or cannot execute it"
      fi
   fi
}

stop_ntpd () {
   if [ ${#ntpd_pid[@]} -gt 0 ]; then
      index=0
      until [ ${index} -ge ${count} ]; do
         pid=${ntpd_pid[${index}]}
         ${LOGGER} "${PROG}: killing pid ${pid}"
         kill -TERM ${pid}
         index=`expr $index + 1`
      done
   fi
}

status_ntpd () {
   index=0
   NTPD_PID=
   until [ ${index} -ge ${count} ]; do
      NTPD_PID="${ntpd_pid[${index}]} ${NTPD_PID}"
      index=`expr $index + 1`
   done

   echo "${PROG} running, pid(s): ${NTPD_PID}"
}

# If we do not have enough arguments, then 
if [ $# -lt 1 ]; then
   usage
else 
   while getopts f:h arg; do
      case ${arg} in
         f)
            NTPD_USER=${OPTARG}
            ;;

         h)
            usage
            ;;

         *)
            usage
            ;;
      esac
   done

   if [ ${UID} -ne 0 ]; then
      printf "${PROG}: needs to have root privileges\n"
      exit 1
   fi

   arg=`echo $* | ${AWK} '{ print $NF; }'`

   case ${arg} in
      start)
         start_ntpd
         ;;

      stop)
         stop_ntpd
         ;;

      status)
         status_ntpd
         ;;

      restart)
         stop_ntpd
         start_ntpd
         ;;

      *)
         usage
         ;;

   esac
fi
